<%@page import="entity.Address"%>
<%@page import="dao.AddressDao"%>
<%@page import="java.util.List"%>
<%@page import="entity.Member"%>
<%@page import="dao.MemberDao"%>
<%@page import="dao.BookDao"%>
<%@page import="entity.Book"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" charset="utf-8" />
        <script src="js/jquery-3.4.1.min.js"></script>
        <script>
            $(document).ready(function(){
                $("#btn1").click(function(){
                    $.ajax({
                        url:"compute",
                        type:"GET",
                        data:{
                            count:$("#count").val()
                        },
                        success:function(data){
                            var price=$("#price").html();
                            $("#spanid1").html(data);
                            $("#spanid2").html((data*price).toFixed(2));
                        }
                    });
                });
            })
          
        </script>
        <style>
            p{
                display: block;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                width: 70%;
                }
            a{
                    text-decoration:none;
                    
                }
            .shenglue{
        overflow: hidden; 
        white-space:nowrap;
        display:block;
        text-overflow: ellipsis;
    }
    div{border-radius: 10px; }
        </style>
        
    </head>
    <body style=" background-color: whitesmoke">
        <div style="margin-left: 10px">
            <h1 style=" text-align: center; font-size: 70px;">确认订单</h1>
            <%
                Book book=new BookDao().getBookById(Integer.parseInt(request.getParameter("bookid")));

            %>
           
            <div style="position: absolute;right:00px;top:00px;font-size:50px; font-weight:bold;"><a href="Cart.jsp">购物车</a><img src="img/购物车.jpg" style="border-radius: 10px" width="150px" height="130px"></div>

            <form onsubmit="return check()" name="ff" action="bs?bookid=<%=book.getId()%>" method="post">
                <div style=" float: left;width:900px; border: 1px bisque solid; background-color: gainsboro;border-radius: 20px; margin-left:20%">
                   
                    <div  style=" float: left;margin:0px 10px; width: 220px;">
                        <h2><%=book.getBookName()%></h2>
                        <img src="img/<%=book.getCover()%>" style="border-radius: 10px" width="220px" height="300px" alt="<%=book.getBookName()%>">
                    </div><br>
                    <div style="float:left; margin:70px 10px; width:240px;" >
                        <p>编号：<%=book.getId()%></p>
                        <p>作者：<%=book.getAuthor()%></p>
                        <p>数量：<span id="spanid1">1</span></p>
                        <p>单价：<span id="price"><%=book.getPrice()%></span></p>
                        <input type="button" value="修改数量" id="btn1" style="height:35px;">&nbsp;&nbsp;&nbsp;
                        <input type="text" id="count" name="count1" value="1" style=" width: 140px; height:30px; font-size:20px;"><br><br>
                        <a href="gbbi?id=<%=book.getId()%>">详情</a>
                    </div>
                        <div style=" float:left; margin:70px 10px; width:240px;"><span>简介：<%=book.getIntroduction()%></span></div>
                </div>
                <div style=" float: left; margin-left: 20%">
                <h1>实付款：<span id="spanid2"><%=book.getPrice()%></span></h1>
                <h3>选择收货地址：</h3><br>

                <!--获取未被删除的地址-->
                <div id="divid">
                    
                <%
                    Member member=new MemberDao().getMemberByName((String)session.getAttribute("username"));
                    List<Address> list=new AddressDao().getAllUndeletedAddress(member.getId());
                    for(int i=0;i<list.size();i++){
                %>
                <div style=" border:1px cadetblue solid; width: 230px; height: 150px; float: left; margin-left:10px; margin-top: 0px;border-radius: 10px;">
                    <input type="radio" class="aclass" name="address" value="<%=list.get(i).getId()%>" checked="checked">
                    <span class="shenglue">详细地址：<%=list.get(i).getAddress()%></span><br>
                    收件人：<%=list.get(i).getAddressee()%><br>
                    电话：<%=list.get(i).getPhone()%><br>
                    邮编：<%=list.get(i).getZipcode()%>
                </div>
                <%
                    }
                %>
                </div>
                <br><br><br><br><br><br><br><br><br><button style="width:80px; height: 45px; font-size:20px;">支付</button>
                </div>
                
            </form>
                <br><br><br><br><br><br><p></p><p></p><p></p><p></p><p></p>
        </div>
                <script type="text/javascript">
            function check(){
                var patrn = /^(-)?\d+(\.\d+)?$/;
                var b=<%=list.size()%>;
                if(document.ff.count1.value==""){
                    alert("请输入购买数量！");
                    return false;
                }if((patrn.exec(document.ff.count1.value) == null)||(document.ff.count1.value<=0)){
                    alert("请输入有效数字！");
                    return false;
                }
                if(b===0){
                    alert("您还未添加收货地址！");
                    return false;
                }
                
                return true;
            
            }
        </script>
    </body>
</html>